package cn.wolfcode.luowowo.mgrsite.job;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatsCacheService;
import cn.wolfcode.luowowo.cache.vo.StrategyStats;
import com.sun.xml.internal.ws.api.server.SDDocumentSource;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

//统计数据的持久化  任务调度
//@Component//交给spring 容器管理
public class StatsDataPersistenceJob {

    @Reference
    private IStrategyStatsCacheService strategyStatsCacheService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    /*
            cron:周期表达式,不能乱写 氧气参数教案中给出的博科
            0/10 * * * * ?:从0秒开始计算 每10秒执行一次
     */
    @Scheduled(cron = "0/3 * * * * ? ")
    public void persistenoe(){
        //核心:从redis中查询数据,人后同步跟新到mysql中
        //查询所有公立的热数据
        List<StrategyStats> vos=strategyStatsCacheService.listAllStrategyStats();
        //遍历vo对象 封装成StrategyDetail对象
        for (StrategyStats vo:vos){
            StrategyDetail detail = new StrategyDetail();
            //获取redis中的数据  封装成detail对象
            detail.setId(vo.getStrategyId());
            detail.setViewnum(vo.getViewnum());
            detail.setThumbsupnum(vo.getThumbsupnum());
            detail.setReplynum(vo.getReplynum());
            detail.setFavornum(vo.getFavornum());

            //3跟新到mysqlzhohg
            strategyDetailService.updateStats(detail);
        }
    }

}
